import java.sql.Statement;
import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

public class Sort {
    public static void insertSort(int[] array) {
        int len = array.length;
        for (int i = 1; i < len; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }


    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    /**
     * 选择排序
     *
     * @param array
     */
    public static void selectSort(int[] array) {
        int len = array.length;
        for (int i = 0; i < len; i++) {
            int minIndex = i;
            for (int j = i + 1; j < len; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            //交换
            int tmp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = tmp;
        }
    }

    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array, left, minIndex);
            //注意可能情况: 最大值刚好在最小值的位置, 已经交换到了minIndex
            if (left == maxIndex) {
                maxIndex = minIndex;
            }
            swap(array, right, maxIndex);

            left++;
            right--;
        }
    }


    /**
     * 时间复杂度: O(n*logN)
     * 堆排序
     */
    public static void heapSort(int[] array) {
        createBigHeap(array);

        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            shiftDown(array, 0, end);
            end--;
        }
    }

    private static void createBigHeap(int[] array) {
        int parent = (array.length - 1 - 1) / 2;
        while (parent >= 0) {
            shiftDown(array, parent, array.length);
            parent--;
        }
    }

    public static void shiftDown(int[] array, int parent, int end) {
        int child = parent * 2 + 1;
        while (child < end) {
            if (child + 1 < end && array[child + 1] > array[child]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array, child, parent);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    /**
     * 冒泡排序
     * 时间复杂度: O(N^2) 最好情况O(N)
     * 稳定性:稳定
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }

    /**
     * 快速排序
     * 时间复杂度:
     * 最好:O(N*logN)
     * 最坏:O(N^2) 逆序/有序
     * 空间复杂度: O(logN)
     * 不稳定
     *
     * @param array
     */
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    //三数取中
    private static int middleNum(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if (array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            } else if (array[mid] > array[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (array[mid] < array[right]) {
                return right;
            } else if (array[mid] > array[left]) {
                return left;
            } else {
                return mid;
            }
        }
    }

    public static void insertSort1(int[] array, int left, int right) {
        for (int i = left + 1; i < right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    private static void quick(int[] array, int start, int end) {
        //优化1: 插入排序
        if (end - start + 1 <= 15) {
            insertSort1(array, start, end);
            return;
        }

        //优化2: 三数取中
        int index = middleNum(array, start, end);
        swap(array, index, start);

        int pivot = partition(array, start, end);
        quick(array, start, pivot - 1);
        quick(array, pivot + 1, end);
    }

    private static int partitionHoare(int[] array, int left, int right) {
        int key = array[left];
        int i = left; //记住最左边的下标
        while (left < right) {
            while (left < right && array[right] >= key) {
                right--;
            }
            //right 下标一定是比 key 小的数
            while (left < right && array[left] <= key) {
                left++;
            }
            //left 下标一定是比 key 大的数

            swap(array, left, right);
        }
        swap(array, left, i);
        return left;
    }

    private static int partitionHole(int[] array, int left, int right) {
        int key = array[left];
        while (left < right) {
            while (left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];

            while (left < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;
        return left;
    }

    //前后指针法
    private static int partition(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while (cur <= right) {
            if (array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array, cur, prev);
            }
            cur++;
        }
        swap(array, prev, left);
        return prev;
    }

    /**
     * 快速排序非递归
     *
     * @param array
     */
    public static void quickSortNor(int[] array) {
        int start = 0;
        int end = array.length - 1;
        Stack<Integer> stack = new Stack<>();
        stack.add(start);
        stack.add(end);
        while (!stack.isEmpty()) {
            end = stack.pop();
            start = stack.pop();
            int pivot = partitionHoare(array, start, end);
            if (start + 1 < pivot) {
                stack.add(start);
                stack.add(pivot - 1);
            }
            if (pivot + 1 < end) {
                stack.add(pivot + 1);
                stack.add(end);
            }
        }
    }

    /**
     * 归并排序
     * 时间复杂度: O(N*logN)
     * 空间复杂度: O(logN)
     * 稳定性: 稳定
     * 目前为止3个稳定的排序: 直接插入排序, 冒泡排序, 归并排序
     */
    public static void mergeSort(int[] array) {
        mergeSortFun(array, 0, array.length - 1);
    }

    private static void mergeSortFun(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int mid = (start + end) / 2;
        mergeSortFun(array, start, mid);
        mergeSortFun(array, mid + 1, end);

        //合并
        merge(array, start, mid, end);
    }

    //这里的思路和合并两个有序数组相似
    private static void merge(int[] array, int left, int mid, int right) {
        int s1 = left;  //可以不定义, 为了方便理解才定义
        int e1 = mid;    //可以不定义, 为了方便理解才定义
        int s2 = mid + 1;
        int e2 = right;  //可以不定义, 为了方便理解才定义
        //定义一个新的数组
        int[] tmpArr = new int[right - left + 1];
        int k = 0; //tmpArr 数组的下标
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            } else {
                tmpArr[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }

        //把排好序的数据拷贝回原来的数组array中
        for (int i = 0; i < tmpArr.length; i++) {
            array[i + left] = tmpArr[i];
        }
    }

    /**
     * 归并排序非递归实现
     */
    public static void mergeSortNor(int[] array) {
        int gap = 1; //每组有几个数据
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i + gap * 2) {
                int left = i;
                int mid = left + gap - 1; //可能会越界
                int right = mid + gap; //可能会越界

                if (mid >= array.length) {
                    mid = array.length - 1;
                }
                if (right >= array.length) {
                    right = array.length - 1;
                }
                merge(array, left, mid, right);
            }

            gap *= 2;
        }
    }

    /**
     * 计数排序
     * 使用场景: 指定范围内的数据
     * 时间复杂度:O(Max(N,范围))
     * 空间复杂度:O(范围)
     * 稳定性:稳定
     * 这里的写法比较简单, 为不稳定的排序
     */
    public static void countSort(int[] array) {
        //先获取数据中的最大值和最小值
        int minVal = array[0];
        int maxVal = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < minVal) {
                minVal = array[i];
            }
            if (array[i] > maxVal) {
                maxVal = array[i];
            }
        }
        //确定计数数组的长度
        int len = maxVal - minVal + 1;
        int[] count = new int[len];

        //遍历array 数组, 把数据出现的此时记录在 count 数组中
        for (int i = 0; i < array.length; i++) {
            count[array[i] - minVal]++;
        }
        //计数数组已经存放了每个数据出现的次数
        //遍历计数数组, 把实际的数据写回array数组
        int index = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                //这里需要重新写回 array
                array[index++] = i + minVal;
                count[i]--;
            }
        }
    }
}


